package com.qkl.xiatang.app.dto.feed;

import com.qkl.xiatang.app.utils.AppDateUtils;
import com.qkl.xiatang.app.utils.Constants;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import java.text.SimpleDateFormat;
import java.time.Period;
import java.util.Date;
import java.util.List;

@Data
public class StatisticsFeedInfoVO {

    private String feedName;

    private Double totalQty;

    private Date startDate;

    private Date endDate;

    public static String showDate(InfoDTO infoDTO){
        if(null == infoDTO || null == infoDTO.minStartDate || null == infoDTO.maxEndDate){
            return "";
        }
        String line = Constants.ENTER_LINE;
        Date minStartDate = infoDTO.minStartDate;
        Date maxEndDate = infoDTO.maxEndDate;
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        String start = format.format(minStartDate);
        String end = format.format(maxEndDate);
        long days = AppDateUtils.between(minStartDate,maxEndDate) + 1;
        return start + line + "~"  + line + end + line + "(共" + days + "天)";
    }

    public static String showFeedInfo(List<StatisticsFeedInfoVO> statisticsList){
        if(ObjectUtils.isEmpty(statisticsList)){
            return "";
        }
        String line = Constants.ENTER_LINE;
        String feedInfo = "";
        for( int i = 0 ,size = statisticsList.size(); i < size ; i++ ){
            if(i == size - 1){
                line = "";
            }
            StatisticsFeedInfoVO vo = statisticsList.get(i);
            feedInfo = feedInfo + vo.feedName + vo.getTotalQty() + "斤" + line;
        }
        return feedInfo;
    }

    public static InfoDTO findMinMaxDate(List<StatisticsFeedInfoVO> statisticsList){
        if(ObjectUtils.isEmpty(statisticsList)){
            return null;
        }
        Date min = null;
        Date max = null;
        for(StatisticsFeedInfoVO vo : statisticsList){
            if(null == min){
                min = vo.startDate;
            }
            if(null != min && null != vo.startDate && min.compareTo(vo.startDate) > 0){
                min = vo.startDate;
            }

            if(null == max){
                max = vo.endDate;
            }
            if(null != max && null != vo.endDate && max.compareTo(vo.endDate) < 0){
                max = vo.endDate;
            }
        }

        InfoDTO infoDTO = new InfoDTO();
        infoDTO.setMaxEndDate(max);
        infoDTO.setMinStartDate(min);
        return infoDTO;
    }

    @Data
    public static class InfoDTO{

        private Date minStartDate;

        private Date maxEndDate;

    }


}
